package com.romotc.qqrobot.webqq.qqInterfaceLog;

import com.romotc.qqrobot.config.bean.QQUrlWatcherConfig;
import com.romotc.qqrobot.webqq.bean.Poll2_ResponseBody;
import com.romotc.qqrobot.webqq.bean.Poll2_ResponseBody_Result;
import com.romotc.scache.RequestSpy;
/**
 * 消息处理
 * @author Administrator
 *
 */
public abstract class Poll2_Processor_Log implements QQUrlLogInterface {

	/**
	 * 消息类型
	 */
	public static String Key_File_message = "file_message";
	public static String Key_Message = "message";
	public static String Key_AllType = "allType";

	@Override
	public final boolean logRequestSpy(RequestSpy rqSpy, QQUrlWatcherConfig config) {

		if(checkSkipLog(rqSpy, config)) {
			return true;
		}
		String resBody = rqSpy.getResBody();
		Poll2_ResponseBody response = null;
		try {
			response = Poll2_ResponseBody.parseFromJsonStr(resBody);
			System.out.println();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			System.out.println("Jackson 解析错误:" + resBody + " request:" + rqSpy.getURL());
			e1.printStackTrace();
			
		}
		
		if(response != null) {
			processPollMsg(rqSpy, config, response);
		}
		return true;
	}

	
	/**
	 * 处理接受到的消息
	 * @param rqSpy
	 * @param config
	 * @param response
	 */
	void processPollMsg(RequestSpy rqSpy, QQUrlWatcherConfig config,
			Poll2_ResponseBody response) {
		if(response.getResult() != null && response.getResult().size() > 0) {
			String pollType = getPollType();
			for(int i = 0; i < response.getResult().size();i++) {
				
				Poll2_ResponseBody_Result result = response.getResult().get(i);
				
				if(pollType != null 
						&& (pollType.equals(result.getPoll_type())) || pollType.equals(Key_AllType))
					processPollResult(rqSpy, config, result,response, i);
			}
		}else {
			processPollEmpty(rqSpy, config, response);
		}
	}

	/**
	 * 需要处理的消息的pollType类型 如果返回非null，则只有匹配该pollType的才调用processPollResult处理
	 * @return
	 */
	abstract String getPollType() ;

	/**
	 * response.result为null 或者为空时调用
	 * @param rqSpy
	 * @param config
	 * @param response
	 */
	abstract void processPollEmpty(RequestSpy rqSpy, QQUrlWatcherConfig config,
			Poll2_ResponseBody response) ;

	/**
	 * response.result不为空时调用
	 * @param rqSpy
	 * @param config
	 * @param result
	 * @param response
	 * @param resultIdx
	 */
	abstract void processPollResult(RequestSpy rqSpy, QQUrlWatcherConfig config,
			Poll2_ResponseBody_Result result, Poll2_ResponseBody response, int resultIdx);

}
